package cm.config.config;

import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yxh
 * @Description: Kafka相关配置
 * @Date: Created in 2025/2/28 13:26
 * @Modified By:
 */
@Configuration
@EnableKafka
public class KafkaConfig {

    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Value("${spring.kafka.producer.retries}")
    private Integer retries;

    @Value("${spring.kafka.producer.batch-size}")
    private Integer batchSize;

    @Value("${spring.kafka.producer.buffer-memory}")
    private Integer bufferMemory;
    /**
     * *单个Kafka使用Integer底层自动处理转换
     * *多个动态获取需要设置String
     */
    @Value("${spring.kafka.producer.acks}")
    private String acks;

    @Value("${spring.kafka.producer.linger-ms}")
    private Integer lingerMs;
//    /**
//     * *序列化定义对象
//     */
//    @Value("${spring.kafka.producer.value-serializer}")
//    private  Class<?> valueSerializer;



//    @Value("${spring.kafka.consumer.group-id}")
//    private String groupId;
//
//    @Value("${spring.kafka.consumer.auto-offset-reset}")
//    private String autoOffsetReset;
//
//    @Value("${spring.kafka.consumer.max-poll-records}")
//    private Integer maxPollRecords;
//
//    @Value("${spring.kafka.consumer.batch-concurrency}")
//    private Integer batchConcurrency;
//
//    @Value("${spring.kafka.consumer.enable-auto-commit}")
//    private Boolean autoCommit;
//
//    @Value("${spring.kafka.consumer.auto-commit-interval}")
//    private Integer autoCommitInterval;
//    /**
//     * *反序列化定义对象
//     */
//    @Value("${spring.kafka.consumer.value-deserializer}")
//    private  Class<?> valueDeserializer;

    /**
     * *用户名
     */
    @Value("${spring.kafka.userName}")
    private String userName ;
    /**
     * *密码
     */
    @Value("${spring.kafka.password}")
    private String password ;

    /**
     * 生产者配置信息
     */
    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.ACKS_CONFIG,acks);
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.RETRIES_CONFIG, retries);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
        props.put(ProducerConfig.LINGER_MS_CONFIG, lingerMs);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        //设置SASL连接
        String saslConf = "org.apache.kafka.common.security.scram.ScramLoginModule required" +
                " username=\"" + userName + "\"" + " password=\"" + password + "\";";
        props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
        props.put(SaslConfigs.SASL_MECHANISM, "SCRAM-SHA-256");
        props.put(SaslConfigs.SASL_JAAS_CONFIG, saslConf);
        return props;
    }

    /**
     *  生产者工厂
     */
    @Bean
    public ProducerFactory<String, String> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    /**
     *  生产者模板
     */
    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }


//    /**
//     *  消费者配置信息
//     */
//    @Bean
//    public Map<String, Object> consumerConfigs() {
//        Map<String, Object> props = new HashMap<>();
//        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
//        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
//        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
//        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecords);
//        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, autoCommit);
//        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 30000);
//        props.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG, 30000);
//        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
//        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializer);
//
//
//        //设置SASL连接
//        String saslConf = "org.apache.kafka.common.security.scram.ScramLoginModule required" +
//                " username=\"" + userName + "\"" + " password=\"" + password + "\";";
//        props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
//        props.put(SaslConfigs.SASL_MECHANISM, "SCRAM-SHA-256");
//        props.put(SaslConfigs.SASL_JAAS_CONFIG, saslConf);
//        return props;
//    }
//
//    /**
//     *  消费者批量工厂
//     */
//    @Bean
//    public KafkaListenerContainerFactory<?> batchFactory() {
//        ConcurrentKafkaListenerContainerFactory<Integer, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
//        factory.setConsumerFactory(new DefaultKafkaConsumerFactory<>(consumerConfigs()));
//        //设置并发量，小于或等于Topic的分区数
//        factory.setConcurrency(batchConcurrency);
//        //消费监听接口监听的主题不存在时，默认会报错，所以设置为false忽略错误
//        factory.setMissingTopicsFatal(false);
//        factory.getContainerProperties().setPollTimeout(1500);
//        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
//        //设置为批量消费，List接收，每个批次数量在Kafka配置参数中设置ConsumerConfig.MAX_POLL_RECORDS_CONFIG
//        factory.setBatchListener(true);
//        return factory;
//    }

}
